define([ "app", "jquery", "md5", "ecb", "services/UserService",
		"resources/UserResource", "resources/EditAuthorityResource" ],
		function(app) {

			var deps = [ "$scope", "$state", "$window","chooseUser","chooseUserName","companyCode","$modalInstance", "$location", "$stateParams", "UserService", "UserResource", "EditAuthorityResource" ];

			function controller($scope, $state, $window,chooseUser,chooseUserName,companyCode, $modalInstance, $location, $stateParams, UserService, UserResource, EditAuthorityResource) {

				// 当前选择修改的用户ID
				$scope.chooseUserId = chooseUser;
				// 当前选择修改的用户名称
				$scope.editUserNme = chooseUserName;
				// 默认不选择其中一个权限
				$scope.departmentFlag=1;
				$scope.company=1;
				$scope.company_local=1;
				// 选择类型 1:部门 2：企业 3:本地+企业,默认不选择
				$scope.chooseType=0;
		        
				/**
		         * 关闭窗口
		         */
				$scope.close = function() {
					$modalInstance.close();
				};
				
				/**
				 * 初始化用户权限
				 */
				initUserAuthority();
				/**
				 * 更新权限
				 */
				$scope.updateAuthority=function() {
					
					EditAuthorityResource.updateAuthority($scope.chooseUserId,companyCode,$scope.chooseType,$scope.timeChoose).success(function(data){
						// 初始化页面
						//var userAuthority =data;
						$modalInstance.close($scope.chooseUserId);
		            });
				};

				/**
				 * 初始化用户权限-电话会议权限
				 */
				function initUserAuthority(){
					EditAuthorityResource.queryUserAuthority($scope.chooseUserId,companyCode).success(function(data){
						// 初始化页面
						var range =data.confAuthorityArea;
						// 部门-默认不选择
			        	var obj=document.getElementById("departmentNoChooose");
			        	// 企业-默认不选择
			    		var obj1=document.getElementById("companyNoChooose");
			    		// 本地+企业-默认不选择
			    		var obj2=document.getElementById("company_localNoChooose");
			    		$scope.timeChoose=data.confCallTime/60;
			    		
						// 本部门
						if(range==1){
		        			$scope.departmentFlag=2;
		        			$scope.company=1;
		        			$scope.company_local=1;
		        			$scope.chooseType=1;
		        			obj.src="images/department.png";
		        			obj1.src="images/company1.png";
			        		obj2.src="images/company_local1.png";
						}
						// 企业通讯录
						if(range==2){
							$scope.departmentFlag=1;
		        			$scope.company=2;
		        			$scope.company_local=1;
		        			$scope.chooseType=2;
		        			obj.src="images/department1.png";
		        			obj1.src="images/company.png";
			        		obj2.src="images/company_local1.png";
						}
						
						// 本地
						if(range==3){
							$scope.departmentFlag=1;
		        			$scope.company=1;
		        			$scope.company_local=2;
		        			$scope.chooseType=3;
		        			obj.src="images/department1.png";
		        			obj1.src="images/company1.png";
			        		obj2.src="images/company_local.png";
						}
		            });
		        }

				/**
				 * 切换选中图片 
				 * type :1部门 2：企业 3：本地
				 */
				$scope.changeImage =function(type){
					// 部门-默认不选择
		        	var obj=document.getElementById("departmentNoChooose");
		        	// 企业-默认不选择
		    		var obj1=document.getElementById("companyNoChooose");
		    		// 本地+企业-默认不选择
		    		var obj2=document.getElementById("company_localNoChooose");
		        	if(type==1){
		        		if($scope.departmentFlag==1){
		        			$scope.chooseType=1;
		        			$scope.departmentFlag=2;
		        			$scope.company=1;
		        			$scope.company_local=1;
		        			obj.src="images/department.png";
		        		}else{
		        			$scope.departmentFlag=1;
		        			$scope.company=1;
		        			$scope.company_local=1;
		        			obj.src="images/department1.png";
		        		}
		        		obj1.src="images/company1.png";
		        		obj2.src="images/company_local1.png";
		        	}
		        	if(type==2){
		        		if($scope.company==1){
		        			$scope.chooseType=2;
		        			$scope.company=2;
		        			$scope.departmentFlag=1;
		        			$scope.company_local=1;
		        			obj1.src="images/company.png";
		        		}else{
		        			$scope.company=1;
		        			$scope.departmentFlag=1;
		        			$scope.company_local=1;
		        			obj1.src="images/company1.png";
		        		}
		        		obj.src="images/department1.png";
		        		obj2.src="images/company_local1.png";
		        	}
		        	
		        	if(type==3){
		        		if($scope.company_local==1){
		        			$scope.chooseType=3;
		        			$scope.company_local=2;
		        			$scope.departmentFlag=1;
		        			$scope.company=1;
		        			obj2.src="images/company_local.png";
		        		}else{
		        			$scope.company_local=1;
		        			$scope.departmentFlag=1;
		        			$scope.company=1;
		        			obj2.src="images/company_local1.png";
		        		}
		        		obj.src="images/department1.png";
		        		obj1.src="images/company1.png";
		        	}
		        };
		        
			}
			controller.$inject = deps;
			return app.lazy.controller("EditCauthorityController", controller);
		});
